package huas.ant.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import huas.ant.common.PageResult;
import huas.ant.entity.Lesson;
import huas.ant.mapper.LessonMapper;
import huas.ant.service.LessonService;
import huas.ant.vo.LessonVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LessonServiceImpl implements LessonService {
    @Autowired
    private LessonMapper lessonMapper;
    /**
     * 分页列表查询
     */
    @Override
    public PageResult page(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        Page<Lesson> p = lessonMapper.page();

        return new PageResult(p.getTotal(),p.getResult());
    }

    /**
     * 添加课程
     */
    @Override
    @Transactional
    public void insert(Lesson lesson) {
        lessonMapper.reset();
        lessonMapper.insert(lesson);
    }
    /**
     * 修改课程
     */
    @Override
    public void update(Lesson lesson) {
        lessonMapper.update(lesson);
    }
    /**
     * 删除课程
     */
    @Override
    public void delete(Integer id) {
        lessonMapper.delete(id);
    }

    /**
     * 回写课程的详细信息
     */
    @Override
    @Transactional
    public LessonVO selectDetails(Integer id) {
        String coachName = lessonMapper.selectCoach(id);
        List<String> memberName = lessonMapper.selectDetails(id);
        LessonVO lessonVO = LessonVO.builder()
                .coachName(coachName)
                .memberName(memberName)
                .build();
        return lessonVO;
    }

}
